Caves were not only a place to live, they always were a place to worship god, gods or whatever. The Mithras cult of ancient Rome was using solely caves for their divine service. So there was only one step from using a cave to the integration of a church in a cave.
Here are some examples of cave churches. Most of them are in Europe.