I have a brand new Trek Marlin 5 (1-2 months old) with hydraulic disc brakes and the rear brake doesn’t work well and when I pull the brake lever, it clicks once somewhere in the frame (cables inner routed).
First, don't ride the bike if one of the brakes is not working properly. Not working well can change to not working at all and put you in a dangerous situation.
Take the bike back to the store where you bought it, a 2 month old bike with a major brake problem should be fixed under warranty. In any case if you do not have experience diagnosing and repairing hydraulic brakes you should have a professional address this problem.
The problem is likely that the rear brake has lost hydraulic fluid, or perhaps a seal has broken in the lever or caliper.