Apparently the DA who fucked up and gave Cosby immunity in 2005... was Trump's defense lawyer in his 2nd impeachment trial
I dunno, there's 2 ways of looking at it;
1) He was corrupt and gave Cosby a sweetheart immunity deal for cash / influence / some other favour
2) Rape cases are hard to prove criminally beyond reasonable doubt, "date rape" cases even more so, and that's when you have obvious douchebag fratboy as a defendant, not lovable family man Dr Cliff Huxtable, and with no physical evidence of the crime it would just be his word against hers, and he wouldn't even have to testify or be cross examined due to the fifth amendment.
Giving an immunity from criminal prosecution meant he could be subpoenaed, deposed, and cross examined, and wouldn't be able to refuse to answer because he was no longer facing criminal charges.
There's also a lower burden of proof in civil cases - you don't have the stricter 'beyond reasonable doubt' hurdle to meet, you just have to make a convincing enough argument that yeah, they did it.
Those testimonies (which showed he was a rapist) could then also be used in other civil cases by other victims whose attacks had fallen outside the statute of limitations, and they could also be made public record so that people know he is a rapist, and prevent possible future offences.
The second reason was the rationale given for the immunity, and it seems pretty strong if you're a DA and you don't think the case you've got in winnable, and some kind of justice (financial compensation of victims and public knowledge of what you are) is better than none at all (or worse, being exonerated in court when a not guilty comes in).