The kidney is capable of excreting acid (as ammonium chloride or NH4Cl) and generating bicarbonate in the process.
This is the way the kidney compensates for a primary respiratory acid-base disturbance or attempts to correct a primary metabolic disturbance (if the kidney itself is not the problem!).
The kidney excretes acid primarily by ammoniagenesis. This is accomplished when the proximal tubules primarily (but also the ascending limb of the loop of Henle) and produces ammonium from glutamine (an amino acid). Ammonium (NH4+) is then excreted by the apical or luminal Na/H exchanger, which functions also as an NH4/H exchanger, absorbing sodium in exchange for ammonium. The ammonium (NH4+) cannot cross membranes and stays in the fluid and is excreted with filtered chloride or reclaimed in the ascending limb of the loop of Henle via the Na-K-2Cl carrier.
In contrast, any ammonia (NH3) produced in this process (not shown) is freely diffusible and enters the renal interstitium in the distal part of the proximal tubule (but not the thick ascending limb of the loop of Henle, which is impermeable to ammonia and water).
Although ammoniagenesis occurs mostly in the proximal tubules, ammonia in the interstitium of the kidney can move freely back into the filtrate or lumen in the distal nephron, where it combines with hydrogen secreted actively into the lumen by vacuolar type H-ATPases in type A intercalated cells. Chloride passively follows hydrogen, mostly through a paracellular pathway. The energy for the H-ATPase is provided by a basolateral chloride/bicarbonate exchanger, so for each hydrogen (and chloride) excreted, bicarbonate is retained. The hydrogen combines with ammonia and filtered or paracellular leaked chloride and is excreted as ammonium chloride.
Acidemia (from a primary respiratory acidosis or primary metabolic acidosis) will increase renal ammoniagenesis (via decreasing intracellular pH) and cause H-ATPases to move from the cytosol to the luminal membrane of the type A intercalated cell, thus promoting acid excretion (with chloride) and bicarbonate retention (i.e. a compensatory or corrective secondary metabolic alkalosis). Conversely, renal ammoniagenesis and H-ATPase activity decreases in metabolic alkalosis, promoting acid (and chloride) retention.