See the following verse from the Manu Smriti :
Manu Smriti 11.9. (If) an opulent man (is) liberal towards strangers,
while his family lives in distress, that counterfeit virtue will first
make him taste the sweets (of fame, but afterwards) make him swallow
the poison (of punishment in hell)
Now, this verse talks about an opulent man but your question is about a man who is already in distress. So, this verse is not immediately applicable to the scenario. But it still can be adapted to your situation.
The verse means that the first duty is to look after the family. Only then one can donate to others. But , if a person is already having loans on him, implies that he and his family is in a financially distressed condition.
In that situation his first priority will be to clear the loans and make his and his family's life easy. But instead of doing that if he continues to donate then he is only adding to the financial distress he is already having and thereby neglecting his duties towards his family.
So, it will be bad karma (or mixed karma) IMO and more so because he is donating with the ulterior motive of earning punya or merits only.