Joshua 2:1 simply says “[the spies] came into the house of a prostitute whose name was Rahab and lodged there”. No reason or motive is stated for them choosing the house of Rahab, so we can only speculate.
There is one practical reason for choosing the house of a prostitute. It would not be unusual for strangers to be admitted into a prostitutes house, even late at night, whereas it might be considered suspicious if an ordinary citizen of Jericho opened up their home to a foreigner. It is possible that the spies, wanting a place to hide, simply choose the place that was most likely to admit them.
We cannot rule out the possibility that the spies had learnt of Rahab’s faith before entering her house, but she introduces the topic in verse nine as though this was the first time that has discussed it.
Perhaps here we should see the work of providence, that brought the spies into the very house where they would be received and protected, which would also lead to the salvation of Rahab.