Gastroenteritis, also called the stomach flu, can cause foamy diarrhea. This happens when infection in the gastrointestinal tract leads to gas bubbles that are passed along with loose stools.