You could use the internal __dict__ of the torchvision.datasets module, which is a bit hacky and most likely not the best approach:
dataset_var = 'MNIST'
dataset = datasets.__dict__[dataset_var](
    root='./data'
)

Alternatively, just check dataset_var for a particular string and load the corresponding dataset after the if-condition.