We have guidance on how to avoid asking an XY question. What is the recommended way to answer an XY question?
I'd avoid answering such questions as the chances of being misunderstood are quite high.
The OP might get annoyed that you haven't answered their question - especially if they don't realise that they've asked an XY question. Other users might misconstrue your answer and you end up getting down-voted as you're "not answering the question". On some sites you might end up causing the moderators more work handling "not an answer" flags.
If you do feel that you want to contribute then a comment politely pointing out what the real problem is would be acceptable.
Do not "avoid" answering an XY problem. Instead, explain why it is an XY problem, and then address the Y problem in your answer.
Remember, our goal here is to help people receive answers to their questions. If they are tackling a problem with the wrong approach, showing them the right approach is a lot more useful than "avoiding" the question.
Moreover, if future users face the same question, and take the same (wrong) approach to tackle it, your answer will help guide them along the right path.
We have been reasonably successful with this approach at The Workplace, as seen from some examples here. (Disclosure: some of those examples are my answers.) Our chat room also has had some relevant discussions on this topic.