Underground Dwelling Creatures-What's The Source of These Legends?

All countries have folklore about creatures that dwell underground-things like trolls, goblins, gnomes, etc. Many anthropologists believe that human legends have some basis in fact. Is there any facts to support these stories?
Also, many of these underground creatures appear to be guarding a “treasure”-that humans are constantly trying to steal (dragons are also reputed to guard underground treasure). Were these stories invented to scare people away from actual treasure hoards? I guess most of these creatures are regarded as evil and dangerous-inasmuch as Christian legend places Hell and the Devil as being in underground caverns.

I suspect a lot of the folklore in this area came from miners hearing odd noises deep underground - and the occasional gas pocket. The correlation of underground treasure and mining is pretty obvious, too.