Well, I mean, if you'd rather a political than a mechanics-of-law explanation as to why the law treats two situations differently mo, it's legal because heterosexual couples plus women outside of such couples combine to form a powerful voting bloc, and gender rating, compared to it's absence with no other change in practice, is a benefit to that group.
Specifically, compared to perfect allocation of costs, it's a small subsidy from people outside of opposite-sex couples to opposite-sex couples, whereas not having gender rating with otherwise status quo policies is a large subsidy from women not in opposite-sex couples to men not in opposite-sex couples.
Under status quo policies, opposite sex couples are in practice fairly free to gender-optimize insurance pricing by putting the less expensive to insure driver as primary on the more expensive to insure vehicle, regardless of actual primary driver.
I worked for a car insurance company for ten years and the management would answer, “Because loss history shows that we pay more in claims for insured men.”
So that’s ok but for some reason if it were a particular race they wouldn’t think to charge people
More by race so honestly I don’t know how to answer your question in a logically consistent manner.
But no one said it is was ethical in the case where it is legal, so if that was the intended question, not only is it phrased wrong but it starts from an unsupported premise, to wit, that people in the discussion believe that one is ethical and the other not.
But of course, there could be laws that prohibit linking vehicle insurance cost to gender.
My solution, when I've been married, has been having my wife get insurance, and add me as a driver. But that doesn't matter so much now that I'm older.
Edit: I do agree that discrimination by gender is fundamentally unethical.
Because there is no specific law that bans charging men and women different prices for car insurance. There are specific laws regarding discriminatory advertising with employment and housing.