The Talmud says, in two places: "Rav Naḥman bar Yitzḥak says: A transgression performed for the sake of Heaven is greater than a commandment performed not for its own sake." [Nazir 23b, Horayot 10b]
The discussion implies that both are OK, and that the second will eventually lead to doing mitzvot for their own sake. But Rav Naḥman'a statement just says one is better than the other. It does not say either is good or encouraged. So: Is this behavior good or encouraged?
Example: On Shabbat morning in shul you hear that someone you know is recovering from major surgery alone in his apartment. You tell yourself, "I can't let him spend Shabbat all by himself." So you drive over to his place and spend the afternoon and evening with him, and do Havdalah with him. Is this OK or not? Need a one-bit answer.
Added note: I went to the suggested link, but I am still not sure I understand when avera lishma applies. The gemara clearly talks about what YOU think you are doing (your intention), not what others think or what the actual result of your action turns out to be.