A key difference is that the moon lacks water, which is to be found in the martian poles IIRC.
The more important key fact is that the moon lacks an atmosphere. One of the calculations by "scientists" was that, with the proposed systems, the habitats would suffer oxygen levels incompatible with life.
On mars you bottle the oxygen and let in some CO2. On the Moon you have to fix the balance as you have no CO2 to let in.
The moon may be closer and cheaper to get to and easier to extract colonists from or even exchange people with, but with the impact of 1/6th G on musculature, no atmosphere and virtually no water, plus the same radiation issue as Mars (or more), the Moon is orders of magnitude harder to keep a permanent habitat.
Mars may be further away, hard to get to, more expensive to get to, but the climate is significantly more conducive to life than the Moon.
Also, yes, people could live underground on the Earth. But would the rest of the planet simply let them live a comfortable life when they were all dying above ground? Our species is pretty self destructive. Mars gives distance from the "madding crowd" as well as challenges.
The point is fairly simple. So long as the human race only lives on the third rock from the sun, it is possible that the whole species can be wiped out in one catastrophic event. Once we go beyond the bounds of a single planet, that is no longer a consideration.
It just depends on how you look at it. Species or cost or person.