No, credit card companies aren't giving out rewards at a loss. Better cards have a higher interchange rate, ie the merchant pays more fees to accept a good card.
Hence why cash discounts are a thing (and yes they're legal again).