PWM on PIN 0 with variable duty cycle

Hi guys,
I’m trying to output a PWM and change this duty cycle based on an interrupt for creating audible melodies (ByteBeat) on Pin 0 of the XIAO, but I got only the code for the Arduino Mega.
The interrupt should be triggered at 8Khz frequency.

void setup(){
    TCCR2A = _BV(COM2A1) | _BV(WGM21) | _BV(WGM20); // Non Inverting PWM on D11
    TCCR2B = 0b00000001;                            // Clock / 1
    TIMSK2 = 0b00000001;                             // TOIE2 activated

void loop(){

// Interrupt at 8Khz for output pwm
    static int t = 0;
    OCR2A = t & 16; // Update of Timer2 duty cycle 

In my dreams, I would like to switch between DAC and PWM output when I want by pressing a button for example.

I think that can’t be achieved trough DAC output because frequency and duty cycle are related, so the DAC frequency need to be changed every time the duty cycle is changed.

How do u think about that ?