Passive buzzer works with analogWrite(), but not with digitalWrite(). It also has constant static noise I have a YL-44/MH-FMD passive piezo buzzer hooked up to an ESP8266/NodeMCU.  

It's connected like this: Buzzer ---> NodeMCU
- GND ---> G
- VCC ---> 3V
- I/O ---> D2  

This code makes a clear beep.

void loop() {
  analogWrite(BUZZER, 255);
  delay(100);
  analogWrite(BUZZER, 0);
}


This code makes a weird low clicking sound, no beep.  

void loop() {
  digitalWrite(BUZZER, HIGH);
  delay(100);
  digitalWrite(BUZZER, LOW);
}


I have no idea why the second one doesn't work, as all the tutorials reference that one.  

Also, why does the buzzer have a weird constant static noise and gets hot? 

Am I doing something wrong in the wiring? I use no resistors, as the tutorials don't show that.
To sound a passive buzzer, use Arduino tone() function. If you set the pin HIGH the piezo surfaces are pulled together (or pulled away). It is what makes one click but nothing more. To make a tone the input must oscillate at right frequency. PWM is one way how to do a beep. Use tone() function to generate tones of desired frequency.

Your analogWrite(255) is PWM at 1/4 of full on, because max analogWrite value on esp8266 is at default 1023. So it makes a nice beep with 255.

If you use a speaker or passive piezo with esp8266 you can expect noise generated by WiFi operations. I set the pin as INPUT after tone() to eliminate the noise.