Leading to its vulnerability, why didn't the First Order built the oscillator (the one the Resistance [and Han Solo & co.] blew up) underground in order to protect it from air strikes and to hide it?
Is it a necessity for it to be on the surface?
One thing to keep in mind with underground things is, that they usually take a longer time to build and are more cost intense than things on the surface. Thus I think we need to look at the question from the opposite angle: Was there anything speaking against building the oscillator on the surface?
All things considered: NO (reasons a bit further below). Thus we only have higher cost and more time to build it underground but no advantage to do so. Thus it is perfectly fine on the surface.
To explain it a bit. What are the reasons to build something underground? Normally so that it is not seen immediately (hoth base as example), or that it is protected by the planet itself. Aka the thick stone around it. In the case of the oscialltor though that wasn't necessary at all. As we have seen it taking multiple hits by proton torpedoes being left unscathed by them! That is not even a feat a star destroyer can be proud of (or a super star destroyer like in eps 6).
I don't think it was ever mentioned how the oscillator managed to do that. So I go with Episode 5 (at ats) and Episode 6 (death star, super star destroyer,...) and guess that it was a combination of deflectors and armour (could have been only armor though. One point I always found sad with star wars is that it had no deflector effects like star trek had for a long time....thus its hard to say what hits the armor absorbs and what hits the deflectors...At-Ats it was the deflectors according to the translations into my mother language).
This means in essence we have a super weapon that encompasses a whole planet. The oscillator for it could be seen without problems as an enemy would have to get near enough to break through the impenetrable planetary shields (which had to be taken down from the inside after a suicide maneuver by an insane pilot...aka jump into atmosphere)...then there were hordes of tie fighters and weapon batteries. AND the oscillator itself was as good as impenetrable by weapons (proton torpedoes even took out a super star destroyers deflectors and a ramming attack its bridge!). Thus there was neither a need to hide it (as its invulnerable) nor to protect it by the rock around it (as it was invulnerable any way).
What no one took into account was a specific pilot mining the oscillator on the inside and making a gap into its armor belt large enough for a star fighter to fit in (a mistake anyone can make).
One thing that COULD have spoken against the surface would have been that the deflectors would also have to be built on the surface and were attackable (their generators at least normally.......which was the oscillator again). The surface part we know from star destroyers where they were quite exposed. Still though that didn't seem to be the case here. So if the 1st order didn't get better ways to build shields than the empire had.....the oscillator just had armor protecting it (the heaviest I ever saw on star wars then).
So to sum it up: There would have been higher cost involved in building it underground and more work to do there.......and it wouldn't have gotten any advantage to building it on the surface (aside from a specific pilot/smuggler maybe not finding it underground....but then again he just always had a knack to get into the thick of things anyway).
So necessary: No, but favorable: Yes
We aren't told that explicitly
“I can’t prove this, but for this amount of power to be restrained until such time as it is released, or fired, there has to be some new, advanced kind of containment field.” He nodded toward Finn. “Our friend here confirms as much. The question is: What kind of field?”
“I heard that it involved the planet’s own magnetic field,” Finn told him, “and something more.”
“Yes, yes.” Statura was deep in thought. “A planetary magnetic field, even a strong one, would not be enough to contain the amount of energy that we have seen deployed. Also as you say, Finn, there is more involved. I am thinking some kind of oscillating field. If it oscillates rapidly enough, much less energy would be required to sustain it than if it was maintained at a steady state.”
(Alan Dean Foster TFA Novelization)
None of this explains why the oscillator is on the surface. I can speculate on a couple of possible reasons (the further you are from planetary center the more effective the oscillator working with planetary magnetic field would be?) but this is LucasPhysics we are dealing with, so no amount of sane scientific guessing will help.
However, there simply isn't any reason for it NOT to be on the surface.
Remember that the planet is protected with planetary shields, which means that anything with enough firepower to attack the planet ostensibly has enough firepower to damage or destroy most of the installations there anyway.
Here's Admiral Akbar's analysis:
“None of this is possible,” a downcast Ackbar postulated. “While the planet in question may at present be deliberately underdefended, the instant we move forces out of hiding and in its direction, the First Order will realize that we know the location of the weapon. They will mobilize everything in the vicinity to protect it. Their fleet is too large for us to fight our way through. Additionally, despite what Poe theorizes, I would wager they must already have at least a minimal planetary shield in place. Plainly, they can access the energy to support such a defense.” He looked at Finn, whose reply was not encouraging.