I have a two year old daughter who has trouble sleeping at night. I was wondering if it is okay to give her melatonin on occasion?

I am not a. Pediatrician but as a mother something that helped my children was a routine that included a warm bath with lavender scented bath additive. There are a number of lavender bath products on the market. One study showed that this is effective for helping children to sleep.
Generally safe. You would have to talk to your pediatrician/fam med doct for specifics if she has medical conditions, but I have prescribed melatonin 0.5-1mg for children under 4. You have to remember that this med should be taken when it gets dark outside. Also there are typically other issues in 2 yr old like limit setting issues and sleep onset association problems that should be treated before meds.
Melatonin for kids. Recent studies have shown that melatonin can be effective in children, and generally well tolerated. Side effects include excessive sedation, headache, and others. Generally it's recommended to start with the lowest possible dose, about 2.5 mg or less for kids under age 5. You should know that it is a hormone, and could affect other hormones. You should check with your child's doctor first.