Constructing these complex dwellings must have taken a considerable amount of time to plan and build. Fear of invasion could not have been the primary motivation - unless they had several years’ notice.
I think your comment “the weather was better” is the most likely reason for their construction. Good shelter from severe weather and later from invaders.
Fear of barbarians was the prime motivator in at least some of the underground arrangements. Barbarians tended to overrun these areas fairly often, like every few years or marauding bands from beyond the civilized areas would raid. It only takes realization that this is a recurring phenomenon for folks to start to dig when the digging is relatively easy and the underground structures stable. Apparently they survive the endemic earthquakes better than surface structures. Entrances and vents are hidden and Tatars or others find empty towns to sack, no TVs to steal and no women to rape. They were also easier to defend when the bandits did get into them.