In 5e, Specters have Incorporeal Movement 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 Attack of Opportunity? Regardless if it would be intelligent enough to do so, would the AoO happen before it is fully submerged below ground or would the ground protect the Specter?
My guess would be the ground would protect it because once it leaves the 5ft reach of its target, it would be mostly submerged.