Should I not ask my students about their hometown? Recently, I taught a class of five students. I asked for their native place and all  told that their hometown is X, but I didn't tell my reason for asking.
I asked them because of the reason that I can communicate easily with them if they are from Y, since I can speak the language of my hometown Y well.
Although I can communicate in our common language, I cannot communicate as well as I can in Y's language.
My doubt here is whether this would constitute discrimination based on place or is it perfectly fine to ask them?  Is it similar to asking about race, religion, caste, etc., which is generally not recommended?
I thought it would be good to edit my answer to start with a link to an article written from the perspectives of people who have had the question &quot;where are you from?&quot; asked repeatedly and who can explain some of the issues with it much better than I can: https://hbr.org/2020/10/whats-wrong-with-asking-where-are-you-from

No, don't do this the way you describe.
I suspect you're talking about local languages in a nation state where there are many many languages, and yet there is a common national language like Hindi or Mandarin. I don't have any personal experience with this situation, being an English speaker in a primarily English country, but I think there are some more general ideas that I'd recommend you think about.
Three recommendations: first, if you must do so, why not merely relay where you are from? If a student recognizes this location and can infer you can speak an additional language with them, they can make this connection on their own. Avoid asking questions of others that they might be uncomfortable with or that might lead to stereotyping.
Second, avoid playing favorites (or even appearing to) as an instructor. I think it is natural to associate with someone who shares something with you (fans of the same music? same hobby? same language/hometown?) and to treat them differently, as well as to view someone from someplace less common as &quot;different&quot; or &quot;other&quot;. I'd avoid asking questions that categorize people. A safer alternative sometimes used as a mnemonic by instructors is to ask about a more generic &quot;something special&quot;, where students can choose exactly what piece of information to share (that said, not all students really like these get-to-know-you games, myself included).
Third, although I think there are a lot of good reasons to promote and cultivate languages and prevent their loss, I'm not sure the university classroom is the best place. Students who learn concepts from you in a common local language to both of you may have difficulties when they advance and are in other courses (or the &quot;real world&quot;) needing to use the same concepts in a more typical language. Practicing using the language of the course may be better for both of you.