When a villager wakes up or spawns in such environment it gets stuck until either the roof it's stuck in is broken for a path out, or the bed gets broken.
Environment
Villagers waking/spawned on a bed in a house 3 blocks tall with the third/top block being top slabs
Duplicate of MC-146770