In 5e, Specters have the Incorporeal Movement trait, which says:
The specter can move through other creatures and objects as if they were difficult terrain. It takes 5 (1d10) force damage if it ends its turn inside an object.
Therefore, I assume Specters can move through ground as if it were difficult terrain.
Could a specter attack a creature, move into its space, and then move straight down into the ground to avoid an Opportunity Attack?
Regardless of whether it would be intelligent enough to do so, would the OA happen before it is fully submerged below ground? Or would the ground protect the Specter?
My guess would be that the ground would protect it, because once it leaves the 5-foot reach of its target, it would be mostly submerged.